package com.example.maple.ccsclient.model;

import android.content.Context;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.utils.HttpRequestUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by maple on 2016/6/26.
 */
public class AllDataInfoUtils {

	public static String lackUrl = "";
	public static String charUrl = "";
	public static String reportUrl = "";

	HttpRequestUtils mHttpRequestUtils = null;
	Context mContext;

	public AllDataInfoUtils(Context context) {

		mContext = context;
		mHttpRequestUtils = new HttpRequestUtils(mContext);
		charUrl = String.format(mContext.getResources().getString(R.string.http), "index?strs=%s");
		lackUrl = String.format(mContext.getResources().getString(R.string.http), "lack?id=%s");
		reportUrl = String.format(mContext.getResources().getString(R.string.http), "report?strs=%s");
	}

	//获取缺字信息
	public void getLackInfo(int id, final ISendData iSendData) {
		mHttpRequestUtils.getRequestString(String.format(lackUrl, id), new ISendData() {
			@Override
			public void sendData(Object o) {
				String s = (String)o;
				Gson gson = new Gson();
				try {
					DataModel<List<LackModel>> dataModel = gson.fromJson(s, new TypeToken<DataModel<List<LackModel>>>() {
					}.getType());
					iSendData.sendData(dataModel);
				} catch (Exception ex) {
					DataModel<String> dataError = gson.fromJson(s, new TypeToken<DataModel<String>>() {
					}.getType());
					iSendData.sendData(dataError);
				}
			}
		});
	}

	//匹配缺字信息
	public void getReportInfo(String strs, final ISendData iSendData) {
		String info = "";
		try {
			info =	URLEncoder.encode(strs,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		mHttpRequestUtils.getRequestString(String.format(reportUrl, info), new ISendData() {

			@Override
			public void sendData(Object o) {
				String s = (String)o;
				Gson gson = new Gson();
				try {
					DataModel<String> dataModel = gson.fromJson(s, new TypeToken<DataModel<String>>() {
					}.getType());
					iSendData.sendData(dataModel);
				} catch (Exception ex) {
					DataModel<String> dataError = gson.fromJson(s, new TypeToken<DataModel<String>>() {
					}.getType());
					iSendData.sendData(dataError);
				}
			}
		});
	}

	//查询字
	public void getCharPic(final boolean isFirst , String strs, final ISendData iSendData) {

		String info = "";
		try {
			info =	URLEncoder.encode(strs,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		mHttpRequestUtils.getRequestString(String.format(charUrl, info), new ISendData() {
			@Override
			public void sendData(Object o) {
				String s = (String)o;
				Gson gson = new Gson();
				try {
					DataModel<List<PicModel>> dataModel = gson.fromJson(s, new TypeToken<DataModel<List<PicModel>>>() {
					}.getType());
					if(isFirst ){
						if(dataModel.getStatus() == 1){
							dataModel.setInfo(s);
						}
					}
					iSendData.sendData(dataModel);
				} catch (Exception ex) {
					DataModel<String> dataError = gson.fromJson(s, new TypeToken<DataModel<String>>() {
					}.getType());
					iSendData.sendData(dataError);
				}
			}
		});
	}
}


